package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryAccountPlanBillReq;
import com.sg.entity.OmsAccountPlanBill;
import com.sg.service.base.app.MOmsAccountPlanBillService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAccountPlanBillMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAccountPlanBillServiceImpl
        extends ServiceImpl<OmsAccountPlanBillMapper, OmsAccountPlanBill>
        implements MOmsAccountPlanBillService {

    @Autowired
    OmsAccountPlanBillMapper omsAccountPlanBillMapper;

    /**
     * 3-9-02批量增规划费用账单[2854]
     * gen by moon at 8/28/2022, 4:28:59 PM
     */
    @Trace(operationName = "3-9-02批量增规划费用账单")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddAccountPlanBill(List<OmsAccountPlanBill> omsAccountPlanBill) {

        if (omsAccountPlanBill == null || omsAccountPlanBill.size() == 0) {
            return false;
        }

        return super.saveBatch(omsAccountPlanBill.stream().map(item -> {

            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 3-9-02批量查询规划费用账单[3257]
     * gen by moon at 8/28/2022, 4:29:00 PM
     */
    @Trace(operationName = "3-9-02批量查询规划费用账单")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAccountPlanBill> batchQueryAccountPlanBill(BatchQueryAccountPlanBillReq reqDto) {

        try {
            QueryWrapper<OmsAccountPlanBill> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            if (reqDto.getAccountPlanBillList() == null || reqDto.getAccountPlanBillList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("plan_account_id", reqDto.getAccountPlanBillList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAccountPlanBill.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAccountPlanBill.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAccountPlanBill.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAccountPlanBill>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-9-02批量删规划费用账单[3258]
     * gen by moon at 8/28/2022, 4:29:00 PM
     */
    @Trace(operationName = "3-9-02批量删规划费用账单")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteAccountPlanBill(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

}

